package cn.mw.cmdb.template.strategy.manager;

import cn.mw.cmdb.template.enums.TemplateDetailEnum;
import cn.mw.cmdb.template.param.MwTemplateConfigurationParam;
import cn.mw.cmdb.template.strategy.TemplateDetailHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @description 模板明细策略管理
 */
@Component
@Slf4j
public class TemplateDetailManage {

    Map<TemplateDetailEnum, TemplateDetailHandler> handlerMap = new HashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        Map<String, TemplateDetailHandler> beans = applicationContext.getBeansOfType(TemplateDetailHandler.class);
        for (TemplateDetailHandler bean : beans.values()) {
            TemplateDetailEnum detailEnum = bean.handlerType();
            handlerMap.put(detailEnum,bean);
        }
    }

    public Object getData(TemplateDetailEnum templateDetailEnum,MwTemplateConfigurationParam param){
        if(templateDetailEnum == null ||  handlerMap.get(templateDetailEnum) == null){
            log.info("TemplateDetailManage{} getData() templateDetailEnum>>>>"+templateDetailEnum+">>>>>handlerMap>>>>"+handlerMap);
            return null;
        }
        return handlerMap.get(templateDetailEnum).getData(param);
    }

    public void addData(TemplateDetailEnum templateDetailEnum,MwTemplateConfigurationParam param){
        if(templateDetailEnum == null ||  handlerMap.get(templateDetailEnum) == null){
            log.info("TemplateDetailManage{} addData() templateDetailEnum>>>>"+templateDetailEnum+">>>>>handlerMap>>>>"+handlerMap);
        }
        handlerMap.get(templateDetailEnum).createData(param);
    }

    public void updateData(TemplateDetailEnum templateDetailEnum,MwTemplateConfigurationParam param){
        if(templateDetailEnum == null ||  handlerMap.get(templateDetailEnum) == null){
            log.info("TemplateDetailManage{} updateData() templateDetailEnum>>>>"+templateDetailEnum+">>>>>handlerMap>>>>"+handlerMap);
        }
        handlerMap.get(templateDetailEnum).updateData(param);
    }

    public void deleteData(TemplateDetailEnum templateDetailEnum,MwTemplateConfigurationParam param){
        if(templateDetailEnum == null ||  handlerMap.get(templateDetailEnum) == null){
            log.info("TemplateDetailManage{} deleteData() templateDetailEnum>>>>"+templateDetailEnum+">>>>>handlerMap>>>>"+handlerMap);
        }
        handlerMap.get(templateDetailEnum).deleteData(param);
    }

    public Object selectDataById(TemplateDetailEnum templateDetailEnum,MwTemplateConfigurationParam param){
        if(templateDetailEnum == null ||  handlerMap.get(templateDetailEnum) == null){
            log.info("TemplateDetailManage{} selectDataById() templateDetailEnum>>>>"+templateDetailEnum+">>>>>handlerMap>>>>"+handlerMap);
            return null;
        }
        return handlerMap.get(templateDetailEnum).selectDataById(param);
    }
}
